package com.heitian.ssm.controller;

import com.heitian.ssm.dao.BathEntityMapper;
import com.heitian.ssm.model.EchartData;
import com.heitian.ssm.model.Series;
import com.heitian.ssm.model.TotalNum;
import com.heitian.ssm.model.Visit;
import com.heitian.ssm.service.BathService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-26
 * Time: 17:20
 * Des:
 */
@Controller
@RequestMapping("/admin")
public class EchartsController {

    private Logger log = Logger.getLogger(EchartsController.class);
    @Resource
    private BathService bathService;
    @Autowired
    private BathEntityMapper bathEntityMapper;


     /*   @Autowired
        private TotalNumBiz toatlNumBiz;
        @Autowired
        private VisitBiz visitBiz;
*/

        @RequestMapping("/showEchartLine")
        @ResponseBody
        public EchartData lineData() {
            System.out.println("折线图");
            List<String> category = new ArrayList<String>();
            List<Long> serisData=new ArrayList<Long>();
            List<TotalNum> list=new ArrayList<TotalNum>();
            TotalNum totalNum0=new TotalNum();
            int i0=bathService.selectByTotalByLineWeek(0);
            totalNum0.setId(1);
            totalNum0.setCount(new Long((long)i0));
            totalNum0.setWeek("当前周");
            TotalNum totalNum1=new TotalNum();
            int i1= bathService.selectByTotalByLineWeek(1);
            totalNum1.setId(2);
            totalNum1.setCount(new Long((long)i1));
            totalNum1.setWeek("前一周");
            TotalNum totalNum2=new TotalNum();
            int i2= bathService.selectByTotalByLineWeek(2);
            totalNum2.setId(3);
            totalNum2.setCount(new Long((long)i2));
            totalNum2.setWeek("前二周");
            TotalNum totalNum3=new TotalNum();
            int i3= bathService.selectByTotalByLineWeek(3);
            totalNum3.setId(4);
            totalNum3.setCount(new Long((long)i3));
            totalNum3.setWeek("前三周");
            TotalNum totalNum4=new TotalNum();
            int i4= bathService.selectByTotalByLineWeek(4);
            totalNum4.setId(5);
            totalNum4.setCount(new Long((long)i4));
            totalNum4.setWeek("前四周");
            TotalNum totalNum5=new TotalNum();
            int i5= bathService.selectByTotalByLineWeek(5);
            totalNum5.setId(6);
            totalNum5.setCount(new Long((long)i5));
            totalNum5.setWeek("前五周");

            list.add(totalNum5);
            list.add(totalNum4);
            list.add(totalNum3);
            list.add(totalNum2);
            list.add(totalNum1);
            list.add(totalNum0);


    /*        List<TotalNum> list = toatlNumBiz.findAll();*/
            for (TotalNum totalNum : list) {
                category.add(totalNum.getWeek());
                serisData.add(totalNum.getCount());
            }
            List<String> legend = new ArrayList<String>(Arrays.asList(new String[] { "总数比较" }));// 数据分组
            List<Series> series = new ArrayList<Series>();// 纵坐标
            series.add(new Series("总数比较", "line", serisData));
            EchartData data = new EchartData(legend, category, series);
            return data;
        }
        @RequestMapping("/showEchartBar")
        @ResponseBody
        public EchartData BarData() {

            System.out.println("柱状图");
            List<String> category = new ArrayList<String>();
            List<Long> serisData=new ArrayList<Long>();


            List<TotalNum> list=new ArrayList<TotalNum>();
            TotalNum totalNum7=new TotalNum();
            SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM");
            String month=matter.format(new Date());
            int i0=bathService.selectByTotalByBarMonth(month);
            log.info("数量"+i0+"日期"+month);
            totalNum7.setCount(new Long((long)i0));
            totalNum7.setWeek(month);
            totalNum7.setId(1);

            Calendar calendar1 = Calendar.getInstance();

            //获取当前时间的前6个月
            TotalNum totalNum1=new TotalNum();
            calendar1.add(Calendar.MONTH,-1);
            Date date01 = calendar1.getTime();
            String month1=matter.format(date01);
            int i1=bathService.selectByTotalByBarMonth(matter.format(date01));
            totalNum1.setId(2);
            totalNum1.setWeek(month1);
            totalNum1.setCount(new Long((long)i1));



            TotalNum totalNum2=new TotalNum();
            Calendar calendar2 = Calendar.getInstance();

            //获取当前时间的前6个月
            calendar2.add(Calendar.MONTH,-2);
            Date date02 = calendar2.getTime();
            String month2=matter.format(date02);
            int i2=bathService.selectByTotalByBarMonth(matter.format(date02));
            totalNum2.setId(3);
            totalNum2.setWeek(month2);
            totalNum2.setCount(new Long((long)i2));          


            TotalNum totalNum3=new TotalNum();
            Calendar calendar3 = Calendar.getInstance();
            //将date类型转换为BigDecimal类型（该类型对应oracle中的number类型）

            //获取当前时间的前6个月
            calendar3.add(Calendar.MONTH,-3);
            Date date03 = calendar3.getTime();
            String month3=matter.format(date03);
            int i3=bathService.selectByTotalByBarMonth(matter.format(date03));
            totalNum3.setId(4);
            totalNum3.setWeek(month3);
            totalNum3.setCount(new Long((long)i3));


            TotalNum totalNum4=new TotalNum();
            Calendar calendar4 = Calendar.getInstance();
            //将date类型转换为BigDecimal类型（该类型对应oracle中的number类型）

            //获取当前时间的前6个月
            calendar4.add(Calendar.MONTH,-4);
            Date date04 = calendar4.getTime();
            String month4=matter.format(date04);
            int i4=bathService.selectByTotalByBarMonth(matter.format(date04));
            totalNum4.setId(5);
            totalNum4.setWeek(month4);
            totalNum4.setCount(new Long((long)i4));


            TotalNum totalNum5=new TotalNum();
            Calendar calendar5 = Calendar.getInstance();
            //将date类型转换为BigDecimal类型（该类型对应oracle中的number类型）

            //获取当前时间的前6个月
            calendar5.add(Calendar.MONTH,-5);
            Date date05 = calendar5.getTime();
            String month5=matter.format(date05);
            int i5=bathService.selectByTotalByBarMonth(matter.format(date05));
            totalNum5.setId(6);
            totalNum5.setWeek(month5);
            totalNum5.setCount(new Long((long)i5));

            list.add(totalNum5);
            list.add(totalNum4);
            list.add(totalNum3);
            list.add(totalNum2);
            list.add(totalNum1);
            list.add(totalNum7);

            for (TotalNum totalNum : list) {
                category.add(totalNum.getWeek());
                serisData.add(totalNum.getCount());
            }
            List<String> legend = new ArrayList<String>(Arrays.asList(new String[] { "总数比较" }));// 数据分组
            List<Series> series = new ArrayList<Series>();// 纵坐标
            series.add(new Series("总数比较", "bar", serisData));
            EchartData data = new EchartData(legend, category, series);
            return data;
        }
        /**
         * 饼状图
         * @param
         * @return
         */
        @RequestMapping("/showEchartPie")
        @ResponseBody
        public EchartData PieData() {
            System.out.println("饼状图");
            List<String> legend = new ArrayList<String>();
            List<Map> serisData=new ArrayList<Map>();

            SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM");
            String month=matter.format(new Date());
            int i0=bathService.selectByTotalBySexMonth(month,"01");
            int i1=bathService.selectByTotalBySexMonth(month,"02");
            int i2=bathService.selectByTotalBySexMonth(month,"05");
            int i3=bathService.selectByTotalBySexMonth(month,"06");

            int boySum=i0+i1+i2+i3;
            int i4=bathService.selectByTotalBySexMonth(month,"03");
            int i5=bathService.selectByTotalBySexMonth(month,"04");
            int i6=bathService.selectByTotalBySexMonth(month,"07");
            int i7=bathService.selectByTotalBySexMonth(month,"08");
            int grilSum=i4+i5+i6+i7;

            Visit totalNum0=new Visit();
            totalNum0.setName("男");
            totalNum0.setValue(boySum);
            totalNum0.setId(1);
            Visit totalNum1=new Visit();
            totalNum1.setName("女");
            totalNum1.setValue(grilSum);
            totalNum1.setId(2);


            List<Visit> list=new ArrayList<Visit>();
            list.add(totalNum0);
            list.add(totalNum1);

          /*  List<Visit> list = visitBiz.findAll();*/
            for (Visit visit : list) {
                Map map =new HashMap();
                legend.add(visit.getName());
                map.put("value", visit.getValue());
                map.put("name",visit.getName());
                serisData.add(map);
            }
            List<Series> series = new ArrayList<Series>();// 纵坐标
            series.add(new Series("总数比较", "pie",serisData));
            EchartData data = new EchartData(legend,null, series);
            return data;
        }


    public static void main(String[] args) throws Exception{
        SimpleDateFormat matter=new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        //将calendar装换为Date类型
        Date date = calendar.getTime();
        //将date类型转换为BigDecimal类型（该类型对应oracle中的number类型）

        //获取当前时间的前6个月
        calendar.add(Calendar.WEEK_OF_YEAR,-6);
        Date date02 = calendar.getTime();
        System.out.print(matter.format(date02));


    }

}
